package com.wwwarehouse.financialcenter.fragment.moneywarehouse.payment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.eventbus_event.LogoutEvent;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.customview.AutoClickButton;

import de.greenrobot.event.EventBus;

import static com.wwwarehouse.common.constant.Constant.sp_Token;

/**
 * @author Joseph on 2017/11/23.
 *         <p/>
 *         支付条例，全屏的webview
 */

public class PaymentWebViewFragment extends BaseFragment implements View.OnClickListener {

    protected BridgeWebView mWebView = null;
    protected String mData;
//    private StateLayout mStateLayout;
    private String mTitle = "";
    private AutoClickButton mBtn ;
    String mUrl = "";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.financial_center_payment_web_view_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ((BaseCardDeskActivity) mActivity).showRefreshBt();
        ((BaseCardDeskActivity) mActivity).hideScanFgBt();
        mWebView = $(R.id.bridge_web_view);
//        mStateLayout = $(R.id.sl_state);
        mBtn = $(R.id.confirm_btn);
        mBtn.setOnClickListener(this);
        mBtn.setEnabled(true);
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));
        String mBaseUrl="" ;
        Bundle bundle = getArguments();
        String url = bundle.getString("url");
        mData = bundle.getString("data");
        mTitle = bundle.getString("title");
        mActivity.setTitle(mTitle);
        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_SIT;
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_BASE_UAT;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_BASE_RELEASE;
        } else if (Constant.ENV_DEV2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_DEV2;
        } else if (Constant.ENV_CIT2.equals(env)) {
            mBaseUrl = Constant.URL_BASE_CIT2;
        }
         mUrl = mBaseUrl + url;
//        mStateLayout.showProgressView(false);
        mWebView.loadUrl(mUrl);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof PaymentWebViewFragment) {
            mActivity.setTitle(mTitle);
        }
    }

    @Override
    public void requestDatas() {
        registJsMethod();
    }

    private void registJsMethod() {
        mWebView.setDefaultHandler(new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                function.onCallBack(mData);
            }
        });

        //显示progressDialog
        mWebView.registerHandler("showProgress", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
//                showProgressDialog("正在加载");
            }
        });

        //隐藏StateLayout和ProgressDialog
        mWebView.registerHandler("hideProgress", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
//                if (mStateLayout.getVisibility() != View.INVISIBLE) {
//                    mStateLayout.setVisibility(View.INVISIBLE);
//                    mStateLayout.showContentView();
//                }
                dismissProgressDialog();
            }
        });

        //跳转到登录页
        mWebView.registerHandler("gotoLogin", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                EventBus.getDefault().post(new LogoutEvent("logout"));
            }
        });

        //获取请求错误信息
        mWebView.registerHandler("showErrorMsg", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
//                mStateLayout.setVisibility(View.VISIBLE);
//                mStateLayout.showSystemView(data, false);
//                mStateLayout.setSystemListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        mWebView.reload();
//                    }
//                });
//                LogUtils.showErrLog("showErrorMsg");
            }
        });
        //数据为空的界面
        mWebView.registerHandler("dataEmptyOperation", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
//                mStateLayout.setVisibility(View.VISIBLE);
//                mStateLayout.showEmptyView(data, false);
//                mStateLayout.setEmptyListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        mWebView.reload();
//                    }
//                });
//                LogUtils.showErrLog("showEmptyView");
            }
        });
        //Js获取请求的参数
        mWebView.callHandler("getData", mData, new CallBackFunction() {
            @Override
            public void onCallBack(String data) {
                ToastUtils.showToast(data);
            }
        });
        mWebView.callHandler("getToken", sp.getValue(sp_Token), new CallBackFunction() {
            @Override
            public void onCallBack(String data) {

            }
        });
    }

    @Override
    public void refreshClick() {
//          mStateLayout.setVisibility(View.VISIBLE);
//          mStateLayout.showProgressView(false);
          mWebView.reload();

    }

    @Override
    public void onClick(View view) {
        if (R.id.confirm_btn == view.getId()) {
            popFragment();
        }
    }
}
